package com.emip.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.UserBean;
import com.emip.dao.BaseDAO;
import com.emip.dao.UserDAO;


@Repository("userDAO")
public class UserDAOImpl extends BaseDAO implements UserDAO{
	
	public UserBean login(String ID,String password)
	{
		String[] params = new String[]{ID,password};
		List<UserBean> result = this.getHibernateTemplate().find("from UserBean u where ID=? and password=?", params);
		if(result.size() == 1)
		{
			System.out.println("get one user");
			return result.get(0);
		}else
		{
			return null;
		}
	}
	
	public String add(UserBean entity)
	{
		String id = (String) this.getHibernateTemplate().save(entity);
		return id;
	}
	
	public void update(UserBean entity)
	{
		this.getHibernateTemplate().update(entity);		
	}
	
	public UserBean findByID(String id)
	{
		UserBean bean = getHibernateTemplate().get(UserBean.class,id); 
		return bean;
	}
	
	public void remove(UserBean entity)
	{
		getHibernateTemplate().delete(entity);
	}

	public List<UserBean> getAll() {
		// TODO Auto-generated method stub
		List<UserBean> result = this.getHibernateTemplate().find("from UserBean m order by ID");
		return result;
	}

	public Boolean findExist(UserBean entity, int mode) {
		Object[] params = new Object[] {entity.getId()};
		List<UserBean> result = this.getHibernateTemplate().find("from UserBean m where ID=?", params);
		if (result.size()==0) {
			return false;
		}
		else {
			if (mode==1) {
				return true;
			}
			else if (mode==2) {
				UserBean eBean = this.getHibernateTemplate().get(UserBean.class, entity.getId());
				if (entity.getId().equals(eBean.getId())) {
					return false;
				} else {
					return true;
				}
			}
			else {return true;}
		}
	}


}
